package com.LiL.Wayne.Activity.fragment;

import java.io.IOException;
import java.util.ArrayList;

import android.graphics.Typeface;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import com.LiL.Wayne.Activity.R;
import com.audamob.audasingers.model.Song;
import com.audamob.audasingers.tool.adapter.VideosAdapter;
import com.audamob.audasingers.tool.adapter.VideosAdapterSimple;
import com.audamob.audasingers.tool.db.CacheReadWriter;
import com.audamob.audasingers.tool.view.ImageResizerUtils;
import com.audamob.data.DataSource;

public class SwipeyTabFragmentFavorites extends Fragment {

	private ArrayList<Song> ListFavorits;
	static SwipeyTabFragmentFavorites f;
	private static boolean[] HideImageImageselection;

	public static Fragment newInstance(String title) {
		f = new SwipeyTabFragmentFavorites();
		Bundle args = new Bundle();
		args.putString("title", title);
		f.setArguments(args);

		return f;
	}

	@Override
	public void onHiddenChanged(boolean hidden) {
		// TODO Auto-generated method stub
		super.onHiddenChanged(hidden);
	}

	ListView ListVideofav;

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		ViewGroup root = (ViewGroup) inflater.inflate(
				R.layout.auda_layout_activity_favorites, null);

		ViewGroup mainContainer = (ViewGroup) root
				.findViewById(R.id.MainContainer);
		Typeface font = Typeface.createFromAsset(getActivity().getAssets(),
				"ExoMedium.otf");

		ImageResizerUtils.setFont(getActivity(), mainContainer, font);

		final String title = getArguments().getString("title");

		ListFavorits = new ArrayList<Song>();
		try {
			ListFavorits = CacheReadWriter.restoreFavoriteList(getActivity());
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (ClassNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		ListVideofav = (ListView) root.findViewById(R.id.ListVideoFavorites);
		ListVideofav.setAdapter(new VideosAdapterSimple(getActivity(), ListFavorits,
				VideosAdapterSimple.MODE_FAVORITES, HideImageImageselection));

		final ImageView iv = (ImageView) root.findViewById(R.id.icon_add_video);
		final TextView addVideo = (TextView) root.findViewById(R.id.Add_video);
		iv.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				if (modeDisplay) {
					modeDisplay = false;
					addVideo.setText("Save selection");
					iv.setBackgroundDrawable(getResources().getDrawable(
							R.drawable.auda_ic_save_favorites));
					ListFavorits = DataSource.getListVideos();
					HideImageImageselection = new boolean[ListFavorits.size()];
					ListVideofav.setAdapter(new VideosAdapterSimple(getActivity(),
							ListFavorits, VideosAdapterSimple.MODE_CHECK,
							HideImageImageselection));
				} else {
					modeDisplay = true;
					addVideo.setText("Add new videos");
					iv.setBackgroundDrawable(getResources().getDrawable(
							R.drawable.auda_ic_add_favorites));
					saveNewFavorites();
					try {
						ListFavorits=CacheReadWriter.restoreFavoriteList(getActivity());
					} catch (IOException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					} catch (ClassNotFoundException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
					ListVideofav.setAdapter(new VideosAdapterSimple(getActivity(),
							ListFavorits, VideosAdapterSimple.MODE_FAVORITES,
							HideImageImageselection));

				}
			}
		});

		return root;

	}

	private boolean modeDisplay = true;

	@Override
	public void onStart() {
		// TODO Auto-generated method stub
		super.onStart();

	}

	public void saveNewFavorites() {
		ArrayList<Song> list = new ArrayList<Song>();
		try {
			list = CacheReadWriter.restoreFavoriteList(getActivity());
		} catch (Exception e) {
			// TODO: handle exception
		}
		for (int j = 0; j < HideImageImageselection.length; j++) {
			if (HideImageImageselection[j]) {

				boolean add = true;
				for (int i = 0; (i < list.size() && add); i++) {
					if (list.get(i).getName()
							.equalsIgnoreCase(ListFavorits.get(j).getName())) {
						add = false;
					}
				}
				if (add) {
					list.add(0, ListFavorits.get(j));
				}
			}
		}
		try {
			CacheReadWriter.sauvegardFavoriteList(list, getActivity());
		} catch (Exception e) {
			// TODO: handle exception
		}
		list.clear();
		ListFavorits.clear();

	}


	@Override
	public void onResume() {
		// TODO Auto-generated method stub
		super.onResume();
	
	}

	
}